﻿#include "CreeperPlant.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Functions/Functions.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char CreeperPlant::getAttackRange() const
    {
        return 5;
    }

    CreeperPlant::CreeperPlant(MonsterInfo *info) : CannibalPlant(info)
    {

    }

    void CreeperPlant::ProcessAI()
    {
        if (!Dead && getEnvir()->getTime() > VisibleTime)
        {
            VisibleTime = getEnvir()->getTime() + 2000;

            bool playersInRange = FindNearby(4);

            if (!Visible && playersInRange)
            {
                Visible = true;
                CellTime = getEnvir()->getTime() + 500;
                Broadcast(GetInfo());
                S::ObjectShow *tempVar = new S::ObjectShow();
                tempVar->ObjectID = ObjectID;
                Broadcast(tempVar);
                ActionTime = getEnvir()->getTime() + 1000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }

            if (Visible && !playersInRange)
            {
                Visible = false;
                VisibleTime = getEnvir()->getTime() + 3000;

                S::ObjectHide *tempVar2 = new S::ObjectHide();
                tempVar2->ObjectID = ObjectID;
                Broadcast(tempVar2);

                SetHP(Stats[Stat::HP]);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }

        CannibalPlant::ProcessAI();
    }

    bool CreeperPlant::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void CreeperPlant::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (!ranged)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            tempVar2->Type = 0;
            Broadcast(tempVar2);
            int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }
}
